package com.example;

import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/StudentDemo",loadOnStartup = 1)
public class servletDemo extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ResultSet resultSet;
        List<FlyUp> list = new ArrayList<FlyUp>();
        try {
            resultSet=servletDemo1();
            while(resultSet.next()){
                int empno = resultSet.getInt(1);
                String ename = resultSet.getString(2);
                String job = resultSet.getString(3);
                int mgp = resultSet.getInt(4);
                Date hiredate = resultSet.getDate(5);
                BigDecimal sal = resultSet.getBigDecimal(6);
                BigDecimal comm = resultSet.getBigDecimal(7);
                int DEPTNO= resultSet.getInt(8);
                FlyUp flyUp = new FlyUp(empno,ename,job,mgp,hiredate,sal,comm,DEPTNO);
                list.add(flyUp);
            };
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.getWriter().write(JSONObject.toJSONString(list));
    }
public ResultSet servletDemo1() throws ClassNotFoundException {
    System.out.println("输出一句话");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/fly_up";
    String username = "blue_genie";
    String password = "123456";
    Connection conn;
    try {
        conn = DriverManager.getConnection(url, username, password);
        String sql = "Select * from emp";
        Statement statement = conn.createStatement();
        ResultSet resultSet =  statement.executeQuery(sql);
        return resultSet;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

}
